<?php 
	require_once('header_sidebar.php');
	require_once('Class/connexionDB.Class.php');
	require_once('Class/displayInHtml.Class.php');
	require_once('Class/session.Class.php');
	
	$session	= session::getInstance();
	$db 		= new connexionDB();
	$date		= date('d-m-Y');

	/**
		Return the current week to monday - friday
	*/
	function getWeek()
	{
		$week 	= array();
		$day  	= date('j');
		$month 	= date('n');
		$year 	= date('Y');
		
		$time	 			= strtotime("monday this week", mktime(0,0,0, $month, $day, $year));
		$week['monday']		= date("Y-m-d", $time);

		$time			 	= strtotime("tuesday this week", mktime(0,0,0, $month, $day, $year));
		$week['tuesday']	= date("Y-m-d", $time);

    	$time			 	= strtotime("wednesday this week", mktime(0,0,0, $month, $day, $year));
    	$week['wednesday']	= date("Y-m-d", $time);

    	$time			 	= strtotime("thursday this week", mktime(0,0,0, $month, $day, $year));
    	$week['thursday']	= date("Y-m-d", $time);
    	
    	$time			 	= strtotime("friday this week", mktime(0,0,0, $month, $day, $year));
    	$week['friday']		= date("Y-m-d", $time);

	    return $week;
	}

	/**
		Order the datas to be displayed 
	*/
	function orderValues($data)
	{
		$result		= array();
		$consultant = '';
		$i 			= 0;

		foreach ($data as $value) 
		{
			if ($consultant != $value['name_consultant'])
			{
				$consultant = $value['name_consultant'];
				$i++;
			}
			$result[$i][] = $value;
		}
		return $result;
	}

	/**
		Return mark from $data by $day
	*/
	function getMarkByDate($day, $data)
	{
		$result = array();

		foreach ($data as $value)
		{
			if ($value['date_eval'] == $day)
			{
				$result['mark'] 		= $value['final_mark'];
				$result['id']	= $value['id'];
			}
		}
		return $result;
	}

	$week 				= getWeek();
	$group 				= explode(';', $session->group);
	$result 			= array();
	$groupsName			= array();
	
	foreach ($group as $value)
	{
		if ($value != '')
		{
			$grp = $db->getGroupNameById($value);
			$grp = $grp[0];
			
			$tmp = $db->getWeeklyReport(	$week['monday'],
											$week['tuesday'],
											$week['wednesday'],
											$week['thursday'],
											$week['friday'],
											'%'.$grp['name'].'%');
			$result = array_merge($result, $tmp);
			$groupsName[] = $grp['name'];
		}
	}
	
	$result 	= orderValues($result);
	$line 		= 0;
?>
	<link rel="stylesheet" href="js/plugins/chosen/chosen.min.css">
	 <aside class="right-side">
		<section class="content-header">
		    <h1>
		        <?php echo $translate->translate('weeklyReport'); ?>
		    </h1>
		    <ol class="breadcrumb">
		        <li><a href="#"><?php echo $translate->translate('weeklyReport'); ?></a></li>
		    </ol>
		</section>
		<section class="content">
			<div class="col-md-12">
				<div class='box box-info col-md-12'>
	                <div class='box-header'>
	                    <h3 class='box-title'>
	                    	<?php 
	                    		echo $translate->translate('reportAdmin') . ' : ' . date('d-m-Y', strtotime($week['monday'])) . ' - ' . date('d-m-Y', strtotime($week['friday'])) . '<br>';
	                    	 	echo '<br> (' . implode(', ', $groupsName).') '; 
	                    	 ?>
	                    	<!--<?php echo $date; ?>-->
	                    </h3>
	                </div>
	                <div class="box-body">
	                    <table id="weekly_report" class="table table-bordered table-hover">
	                    	<thead>
		                    	<tr>
		                    		<th>
		                    			<?php echo $translate->translate('Consultant'); ?>
		                    		</th>
		                    		<?php foreach ($week as $key => $value) : ?>
		                    			<th>
		                    				<?php echo $translate->translate($key) . ' (' . date('d-m', strtotime($value)) . ')'; ?>
		                    			</th>
		                    		<?php endforeach; ?>
		                    		<th>
		                    			<?php echo $translate->translate('averageMark'); ?>
		                    		</th>
		                    		<th>
		                    			<?php echo $translate->translate('nbRating'); ?>
		                    		</th>
		                    	</tr>
	                    	</thead>
	                    	<tbody>
	                    		<?php 
	                    			$totalAvg 	= 0;
	                    			$totalNb 	= 0;
	                    		?>
		                    	<?php foreach ($result as $value) : ?>
		                    		<?php 
		                    			$nb = 0; 
		                    			$total = 0;
		                    		?>
		                    		<tr>
		                    			<td>
		                    				<?php echo $value[0]['name_consultant'] . ' ( ' . $value[0]['groups'] . ' )'; ?>
		                    			</td>
			                    		<?php foreach ($week as $day) : ?>
			                    			<?php 
			                    				$mark_date 	= getMarkByDate($day, $value);
			                    			?>
			                    			<?php if (!empty($mark_date)) : ?>
			                    				<?php 
			                    					$nb++; 
			                    					$total += floatval($mark_date['mark']);
			                    				?>
		 		                    			<td>
				                    				<a id='mark_' href='show_rating.php?id=<?php echo $mark_date["id"]?>' target="_blank"><?php echo $mark_date["mark"]?></a>
				                    			</td>
				                    		<?php else :?>
				                    			<td>
				                    			</td>
				                    		<?php endif; ?>
			                    		<?php endforeach; ?>
			                    		<td id='<?php echo "avg_".$line; ?>'>
			                    			<?php 
			                    				$avg = $total / $nb;
			                    				echo round($avg, 2); 
			                    				$totalAvg += $avg;
			                    			?>
			                    		</td>
			                    		<td id='<?php echo "nb_".$line; ?>'>
			                    			<?php 
			                    				echo $nb;
			                    				$totalNb += $nb;
			                    			?>
			                    		</td>
		                    		</tr>
		                    		<?php $line++; ?>
	                    		<?php endforeach; ?>
	                    		<tr>
	                    			<td colspan="6"><b>Total</b></td>
	                    			<td><?php echo ($line == 0 ? 0 : round(($totalAvg / $line), 2)); ?></td>
	                    			<td><?php echo $totalNb ?></td>
	                    		</tr>
	                    	</tbody>
	                    </table>
	                </div>
	                <div class="box-footer pull-right">
                        <a download="Table_report_weekly.xls" class="btn btn-success" onclick="return ExcellentExport.excel(this, 'weekly_report', 'Table weekly');">
                            <img src='img/excell_export.png' width="10%"/>Export to Excel
                        </a>
                    </div>
	            </div>
	        </div>
		</section>
	</aside>
</div>
<?php require_once('footer.php');?>
<!-- Export Excel -->
<script type="text/javascript" src="js/plugins/excellentexport/excellentexport.min.js"></script>